package com.dw.article.controller;



import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.dw.article.mapper.ee.EeReplyInfoMapper;
import com.dw.article.utils.LoginUtil;
import com.dw.common.enums.ErrorCode;
import com.dw.common.utils.ResultUtils;
import com.dw.model.article.dtos.comment.AddCommentDTO;
import com.dw.model.article.dtos.comment.QueryCommentDTO;
import com.dw.model.article.dtos.reply.QueryReplyDTO;
import com.dw.model.article.entitys.CommentInfo;
import com.dw.model.article.entitys.ReplyInfo;
import com.dw.article.service.ReplyInfoService;
import com.dw.common.entitys.BaseResponse;
import com.dw.model.article.vos.comment.QueryCommentVO;
import com.dw.model.article.vos.reply.QueryReplyVO;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 回复信息表(ReplyInfo)表控制层
 *
 * @author makejava
 * @since 2024-11-08 08:49:19
 */
@Slf4j
@RestController
@RequestMapping("/article/reply")
public class ReplyInfoController {

    @Resource
    private ReplyInfoService replyInfoService;

    @Resource
    private EeReplyInfoMapper eeReplyInfoMapper;

    /**
     * 创建es索引
     */
    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return eeReplyInfoMapper.createIndex("reply_info");
    }

    @PostMapping("/add")
    public BaseResponse<Boolean> add(@RequestBody ReplyInfo replyInfo) {
        log.info("新增回复参数:{}", replyInfo);
        // 设置评论id
        long leiId = IdUtil.getSnowflake().nextId();
        replyInfo.setLeiId(leiId);
        // 设置创建人id
        String loginId = LoginUtil.getLoginId();
        replyInfo.setCreatedBy(Long.valueOf(loginId));
        replyInfoService.save(replyInfo);
        // ee新增评论
        Integer insert = eeReplyInfoMapper.insert(replyInfo);
        return ResultUtils.success(insert > 0);
    }

    /**
     * 根据评论id查询当前文章下的评论
     */
    @PostMapping("/query")
    public BaseResponse<List<QueryReplyVO>> queryComment(@RequestBody QueryReplyDTO queryReplyDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("CommentInfoController queryComment queryCommentDTO:{}", JSON.toJSONString(queryReplyDTO));
            }
            // ee查询当前评论下的所有回复
            LambdaEsQueryWrapper<ReplyInfo> wrapper = new LambdaEsQueryWrapper<>();
            wrapper.eq(ReplyInfo::getCommentId, queryReplyDTO.getCommentId());
            List<ReplyInfo> replyInfos = eeReplyInfoMapper.selectList(wrapper);
            // 分页查询
            List<List<ReplyInfo>> partition = Lists.partition(replyInfos, queryReplyDTO.getPageSize());
            List<ReplyInfo> replyInfoList = partition.get(queryReplyDTO.getCurrent() - 1);
            // 中级查询
            List<QueryReplyVO> queryReplyVOList = replyInfoService.queryReply(replyInfoList);
            return ResultUtils.success(queryReplyVOList);
        } catch (Exception e){
            log.error("CommentInfoController queryComment error:{}",e.getMessage(),e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"根据评论id查询当前评论下的回复报错");
        }
    }
}

